<?php

namespace Home\Controller;


class UserController extends BaseController {

    public function login() {
        if (!empty($_POST)) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $user = new \Model\UserModel();
            $info = $user->where("username='$username'")->find();
            session('user_id',$info['user_id']);
            session('username',$username);
            if ($info) {
                if ($password === $info['password']){
                    $this->redirect('Index/index');
                } else {
                    echo '密码错误!';
                }
            } else
                echo '账号不存在!';
        }

        $this->display();
    }
    function logout(){
          session(null);
          $this->redirect('login');
      }
    function register() {
        $user = new \Model\UserModel();
        if (!empty($_POST)) {
//            dump($_POST);
            $shuju = $user->create(); //收集表单[$_POST]信息并返回,同时触发表单自动验证

            if ($shuju) {
                $shuju['user_hobby'] = implode(',', $shuju['user_hobby']); //把爱好信息由数组转变为字符串
                if ($user->add($shuju)) {
                    $this->redirect('Index/index');
                }
            } else {
                $info = $user->getError(); //输出验证的错误信息
//                     dump($info);
                $this->assign('errorinfo', $info);
                //   dump($info);
            }
        }
        $this->display();
    }
    function showlist(){
        $this->display();
    }
    function order(){
        $this->display();
    }
    function address(){
        $this->display();
    }
}
